/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.toolsgui;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.tree.TreePath;

import sshtunneler.tools.Tool;
import sshtunneler.tools.ToolsManager;
import sshtunneler.toolsgui.tree.ToolTreePropertyNode;
import sshtunneler.toolsgui.tree.ToolTreeRoot;
import sshtunneler.toolsgui.tree.ToolTreeToolNode;

public class ToolConfig extends javax.swing.JDialog implements MouseListener {
	private static final long serialVersionUID = 2435075244565916267L;

	public ToolConfig(JFrame mainFrame) {
		super(mainFrame, true);
		this.initComponents();
		this.jTree1.addMouseListener(this);
		ToolTreeRoot.inst.refresh();
	}

	private void initComponents() {

		this.jScrollPane1 = new javax.swing.JScrollPane();
		this.jTree1 = new javax.swing.JTree(ToolTreeRoot.inst);
		this.jScrollPane2 = new javax.swing.JScrollPane();

		this
				.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Tool configuration");
		this.setModal(true);
		this.setName("Form");
		this.setResizable(false);
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent evt) {
				ToolConfig.this.formWindowClosing(evt);
			}
		});

		this.jScrollPane1.setName("jScrollPane1");

		this.jTree1.setName("jTree1");
		this.jTree1.setPreferredSize(new java.awt.Dimension(200, 64));
		this.jScrollPane1.setViewportView(this.jTree1);

		this.getContentPane().add(this.jScrollPane1,
				java.awt.BorderLayout.CENTER);

		this.jScrollPane2.setName("jScrollPane2");
		this.jScrollPane2.setPreferredSize(new java.awt.Dimension(250, 2));
		this.getContentPane()
				.add(this.jScrollPane2, java.awt.BorderLayout.EAST);

		this.pack();
	}

	private void formWindowClosing(java.awt.event.WindowEvent evt) {
		ToolsManager.inst.save_all_tool_config();
	}

	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTree jTree1;

	public void mouseClicked(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		if (e.getButton() != MouseEvent.BUTTON1) {
			return;
		}
		TreePath path = this.jTree1.getPathForLocation(e.getX(), e.getY());
		if (path != null) {
			Object selected = path.getLastPathComponent();
			if (selected != null) {
				if (selected instanceof ToolTreeToolNode) {
					Tool t = ((ToolTreeToolNode) selected).getTool();
					this.jScrollPane2.setViewportView(new PanelToolEditTool(t));
				} else if (selected instanceof ToolTreePropertyNode) {
					TreePath toolPath = path.getParentPath();
					Object parent = toolPath.getLastPathComponent();
					if (parent != null && parent instanceof ToolTreeToolNode) {
						Tool t = ((ToolTreeToolNode) parent).getTool();
						String key = ((ToolTreePropertyNode) selected).getKey();
						this.jScrollPane2
								.setViewportView(new PanelToolEditProperty(t,
										key));
					}
				}
			}
		}
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}
}
